package com.adinnet.admin.school.controller;


import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.organization.model.Organization;
import com.adinnet.admin.organization.model.vo.OrganizationResp;
import com.adinnet.admin.organization.service.OrganizationService;
import com.adinnet.admin.school.model.School;
import com.adinnet.admin.school.model.vo.SchoolResp;
import com.adinnet.admin.school.service.SchoolService;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 校区 前端控制器
 * </p>
 *
 * @author liukp
 * @since 2020-12-05
 */
@Controller
@RequestMapping("/admin/school")
public class SchoolController extends BaseController {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private CommonService commonService;

    @Autowired
    private OrganizationService organizationService;

    /**
     * @description 查询机构下，管辖的校区列表
     * @author lkp
     * @param
     * @param: organizationId
     * @createTime 2020/12/9 11:38
     */
    @ResponseBody
    @RequestMapping("/getListByOrganizationId")
    public List<School> getListByOrganizationId(Integer organizationId) {
        SysUserInfo sysUserInfo=commonService.getLoginUser();
        return schoolService.getListByOrganizationId(organizationId,sysUserInfo);
    }

    @RequestMapping()
    public String school(Model model) {
        model.addAttribute("organizationList", organizationService.list(new QueryWrapper<Organization>().eq("deleted", 0).orderByDesc("created_date")));
        return "school/school.html";
    }


    /**
     * 查询所有校区
     **/
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<SchoolResp> list(String keyword,Integer page, Integer limit) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        return schoolService.schoolList(keyword,page,limit);
    }

    /**
     * 添加校区
     */
    @RequestMapping("/add")
    @ResponseBody
    @Transactional
    public JsonResult add(School school, HttpServletRequest request) {
        QueryWrapper<School> qw = new QueryWrapper<>();
        qw.eq("deleted", 0).eq("school_name", school.getSchoolName());
        List<School> schools = schoolService.list(qw);
        if (CollectionUtils.isNotEmpty(schools)) {
            return JsonResult.error("校区名称已存在，请更换名称!");
        }
        if (schoolService.save(school)) {
            commonService.saveOperation(request,"新增校区，校区ID："+school.getId());
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改校区
     */
    @RequestMapping("/update")
    @ResponseBody
    @Transactional
    public JsonResult update(School school, HttpServletRequest request) {
        QueryWrapper<School> qw = new QueryWrapper<>();
        qw.eq("deleted", 0).eq("school_name", school.getSchoolName()).notIn("id",school.getId());
        List<School> schools = schoolService.list(qw);
        if (CollectionUtils.isNotEmpty(schools)) {
            return JsonResult.error("校区名称已存在，请更换名称!");
        }
        if (schoolService.updateById(school)) {
            commonService.saveOperation(request,"修改校区，校区ID："+school.getId());
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除校区
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Transactional
    public JsonResult delete(Integer schoolId, HttpServletRequest request) {
        commonService.saveOperation(request,"删除校区，校区ID："+schoolId);
        return schoolService.delete(schoolId);
    }

}
